package com.wenson.behavior.mediator;

import java.util.Objects;

/**
 * @ClassName: PrivateChatRoom
 * @Author: luowangcheng
 * @Date: 2024/6/16 10:33
 * @Description:
 */
public class PrivateChatRoom extends BaseChatRoom {
    public PrivateChatRoom(String name) {
        super(name);
    }

    @Override
    protected void register(User user) {
        if (users.size() == 2) {//聊天室最多容纳2人
            System.out.println("系统消息：聊天室已满");
            return;
        }
        super.register(user);
        System.out.print("系统消息：欢迎【" + user.getName() + "】");
        System.out.println("加入2人聊天室【" + name + "】");
    }

    @Override
    protected void unregister(User user) {
        super.unregister(user);
        System.out.print("系统消息：" + user.getName());
        System.out.println("离开聊天室");
    }

    @Override
    protected void sendMsg(User from, User to, String msg) {
        users.forEach(
                user -> user.listen(from, null, msg)
        );
    }

    @Override
    protected String processMsg(User from, User to, String msg) {
        return from.getName() + "说：" + msg;
    }
}
